package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    private TeacherDao tDao = new TeacherDao();//上游

    //add - 返回接收的工号是否重复
    public boolean checkId(String id) {
        //指挥Dao提供现有学生信息
        Teacher[] ts = tDao.findAllTeacher();
        //判断是否有重复
        boolean flag = false;//没有重复
        for (int i = 0; i < ts.length; i++) {
            if (ts[i] != null && ts[i].getTid().equals(id)){//非空判断,避免空指针异常
                flag = true;
                break;
            }
        }
        //返回结果
        return flag;
    }

    //add -返回是否成功添加
    public boolean addTeacher(Teacher t) {
        //指挥Dao添加进入数据库
        boolean res = tDao.addTeacher(t);
        //返回结果
        return res;

    }

    //view -返回全部数据信息
    public Teacher[] viewTeacher() {
        Teacher[] ts = tDao.findAllTeacher();
        //全为空 - 返回null
        //非空 - 返回ts
        boolean flag = false;
        for (int i = 0; i < ts.length; i++) {
            if (ts[i] != null){//非空
                flag = true;
                break;
            }
        }
        if (flag){
            return ts;
        }else return null;//*易错点 ts存储数组地址,数组内容全为null,则将ts存储的地址丢弃(置为null,链子扔了)
    }

    //delete - 无返回
    public void deleteTeacher(String id) {
        //指挥Dao去操作
        tDao.deleteTeacher(id);
    }

    //update - 无返回
    public void updateTeacher(Teacher t) {
        //指挥Dao去操作
        tDao.updateTeacher(t);
    }
}
